package ch.nostromo.tiffanys.tests.libraries;

import java.util.StringTokenizer;

import ch.nostromo.lib.util.NosTools;
import ch.nostromo.tiffanys.game.GameController;
import ch.nostromo.tiffanys.game.GameControllerException;
import ch.nostromo.tiffanys.game.game.GameIllegalMoveException;
import ch.nostromo.tiffanys.tests.TestBase;

public class TestSan extends TestBase {

  public void testCorrectSan() throws GameControllerException  {
    GameController gc = new GameController();
    
    String moves = "1.e4 d5 2.e5 c5 3.Nc3 d4 4.Na4 Qd5 5.Qe2 Bd7 6.c4 Qc6 7.b3 a6 8.Ba3 b5 9.Nxc5 a5 10.cxb5 Qd5 11.Nxd7 Nxd7 12.Nf3 Rb8 13.Rc1 d3 14.Qxd3 Qxd3 15.Bxd3 e6 16.Bxf8 Kxf8 17.Rc7 Nb6 18.Ng5 Nh6 19.Nxh7+ Ke8 20.Ng5 Nd5 21.Rc4 f5 22.Nxe6 Ke7 23.Nxg7 Rhc8 24.Rf1 Rxc4 25.bxc4 Nf4 26.Nxf5+ Nxf5 27.Bxf5 Nxg2+ 28.Kd1 Rd8 29.Rg1 Nf4 30.Rg7+ Kf8 31.Rh7 Kg8 32.Rh4 Ng2 33.Rg4+ Kf7 34.a4 Rg8 35.e6+ Kf6 36.Rxg8 Nf4 37.Rf8+ Ke5 38.e7 Ne6 39.e8=Q Kd6 40.Qxe6+ Kc5 41.Bc2 Kb4 42.c5 Kxc5 43.Rf4";
    
    moves = NosTools.replaceAllSubString(moves, "\n", " ");
    
    StringTokenizer st = new StringTokenizer(moves, " ");
    while(st.hasMoreTokens()) {
      String move = st.nextToken();
      int point = move.indexOf(".");
      if (point >= 0) {
        move = move.substring(point + 1);
      }

      gc.enterNextMove(move);
    }

  }
  
  public void testWrongSan() throws GameControllerException {
    GameController gc = new GameController();
    
    try {
      String move = "1.e5";
      gc.enterNextMove(move);
      fail ("Move e5 should throw Exception");
    } catch (GameIllegalMoveException expected) {
      // Do nothing
    }
    
    try {
      String move = "1.Na3";
      gc.enterNextMove(move);
      fail ("Move Na3 should throw Exception");
    } catch (GameIllegalMoveException expected) {
      // Do nothing
    }
    
  }
  
}
